package com.ds.web.controller.applet;

import com.ds.applet.domain.MiniPledgeNum;
import com.ds.applet.domain.MiniQualityGuarantee;
import com.ds.applet.service.IMiniPledgeNumService;
import com.ds.applet.service.IMiniQualityGuaranteeService;
import com.ds.common.annotation.Log;
import com.ds.common.core.controller.BaseController;
import com.ds.common.core.domain.AjaxResult;
import com.ds.common.core.page.TableDataInfo;
import com.ds.common.enums.BusinessType;
import com.ds.common.utils.poi.ExcelUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 质保管理Controller
 *
 * @author liucan
 * @date 2024-03-28
 */
@RestController
@RequestMapping("/applet/guarantee")
public class MiniQualityGuaranteeController extends BaseController {

    @Resource
    private IMiniQualityGuaranteeService miniQualityGuaranteeService;

    @Resource
    private IMiniPledgeNumService iMiniPledgeNumService;

    @Resource
    private MiniLeagueController miniLeagueController;

    /**
     * 查询质保管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(@AuthenticationPrincipal UserDetails userDetails, MiniQualityGuarantee miniQualityGuarantee, HttpServletRequest request) {
        startPage();
        miniQualityGuarantee.setToApplet(miniLeagueController.toApplet(request));
        List<MiniQualityGuarantee> list = miniQualityGuaranteeService.selectMiniQualityGuaranteeList(miniQualityGuarantee);
        return getDataTable(list);
    }


    /**
     * 小程序首页录入质保页面中的质保编码查询
     */
    @GetMapping("/selectNum")
    public AjaxResult selectNum(MiniQualityGuarantee miniQualityGuarantee) {
        //查询客户选择的膜类型和产品品牌和产品型号查询  MiniPledgeNum质保编码对象表启用状态 对应的生成规则、位数和最后一个到几号了
        MiniPledgeNum miniPledgeNum = new MiniPledgeNum();
        miniPledgeNum.setDictId(miniQualityGuarantee.getQuTypeId());//产品类型
        miniPledgeNum.setBrId(miniQualityGuarantee.getQuCaBrand());//产品品牌
        miniPledgeNum.setPrId(miniQualityGuarantee.getPrModel());//产品型号
        MiniPledgeNum miniPledgeNum1 = iMiniPledgeNumService.selectMiniPledgeNumList(miniPledgeNum).get(0);
        String coRule = miniPledgeNum1.getCoRule();//生成规则
        Integer coPlace = Math.toIntExact(miniPledgeNum1.getCoPlace());//位数
        Integer coSome = miniPledgeNum1.getCoSome() + 1;//到几号
        String paddedNumber = String.format(coRule + "%0" + coPlace + "d", coSome);
        miniQualityGuarantee.setQuQualityNum(paddedNumber);//质保编号
        miniQualityGuarantee.setCoSome(coSome);
        miniQualityGuarantee.setPnId(miniPledgeNum1.getId());//质保编号的id
        return AjaxResult.success(miniQualityGuarantee);
    }

    /**
     * 新增质保管理
     */
    @Log(title = "质保管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MiniQualityGuarantee miniQualityGuarantee, HttpServletRequest request) {
        miniQualityGuarantee.setToApplet(miniLeagueController.toApplet(request));
        return miniQualityGuaranteeService.insertMiniQualityGuarantee(miniQualityGuarantee);
    }

    /**
     * 修改质保管理
     */
    @Log(title = "质保管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MiniQualityGuarantee miniQualityGuarantee, HttpServletRequest request) {
        miniQualityGuarantee.setToApplet(miniLeagueController.toApplet(request));
        return toAjax(miniQualityGuaranteeService.updateMiniQualityGuarantee(miniQualityGuarantee));
    }

    /**
     * 删除质保管理
     */
    @Log(title = "质保管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(miniQualityGuaranteeService.deleteMiniQualityGuaranteeByIds(ids));
    }

    /**
     * 根据手机号、质保卡号、车架号、车牌号查询
     */
    @GetMapping("/select")
    public AjaxResult select(MiniQualityGuarantee miniQualityGuarantee, HttpServletRequest request) {
        miniQualityGuarantee.setToApplet(miniLeagueController.toApplet(request));
        List<MiniQualityGuarantee> list = miniQualityGuaranteeService.select(miniQualityGuarantee);
        list.forEach(miniQualityGuarantee1 -> {
            System.out.println("miniQualityGuarantee1==="+miniQualityGuarantee1);
        });
        return AjaxResult.success(list);
    }


    /**
     * 导出质保管理列表
     */
    @PreAuthorize("@ss.hasPermi('applet:guarantee:export')")
    @Log(title = "质保管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiniQualityGuarantee miniQualityGuarantee)
    {
        List<MiniQualityGuarantee> list = miniQualityGuaranteeService.selectMiniQualityGuaranteeList(miniQualityGuarantee);
        ExcelUtil<MiniQualityGuarantee> util = new ExcelUtil<MiniQualityGuarantee>(MiniQualityGuarantee.class);
        util.exportExcel(response, list, "质保管理数据");
    }

    /**
     * 获取质保管理详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(miniQualityGuaranteeService.selectMiniQualityGuaranteeById(id));
    }
}
